//
//  WWCollectedCitiesTableViewController.m
//  WhatWeather
//
//  Created by byn on 15/12/27.
//  Copyright © 2015年 byn. All rights reserved.
//

#import "WWCollectedCitiesTVC.h"
#import "WWCollectedCitiesTVCell.h"
#import "AppDelegate.h"
#import "WWCollectedCitiesManage.h"
#import "ShowDetailWeatherTVC.h"

@interface WWCollectedCitiesTableViewController ()

@end

@implementation WWCollectedCitiesTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _collectedCityID = [[WWCollectedCitiesManage sharedCollectedCities] allCollectedCity];
    
    UIImage *img = [UIImage imageNamed:@"blue_mask.png"];
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:img];
    bgImageView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height);
        //[_backView addSubview:bgImageView];
    [self.tableView setBackgroundColor:[UIColor colorWithPatternImage:img]];
}

-(void)viewWillAppear:(BOOL)animated{
    _collectedCityID = [[WWCollectedCitiesManage sharedCollectedCities] allCollectedCity];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _collectedCityID.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *identifer = @"WWCollectedCities";
    WWCollectedCitiesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer forIndexPath:indexPath];
    NSMutableDictionary *cityDic = _collectedCityID[indexPath.row];
    cell.cityName.text = [cityDic objectForKey:@"cityName"];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row >= [_collectedCityID count]) {
        return;
    }
    
    NSMutableDictionary *cityDic = _collectedCityID[indexPath.row];
    [self.mm_drawerController closeDrawerAnimated:YES completion:nil];
    AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
    UINavigationController *navigationController = (UINavigationController*)appDelegate.drawViewController.centerViewController;
    ShowDetailWeatherTVC *detailTVC = navigationController.viewControllers[0];
    NSString *cityId = [cityDic objectForKey:@"cityId"];
    detailTVC.curAreaId = cityId;
    NSString * cityName = [cityDic objectForKey:@"cityWeather"];
    detailTVC.historyWeather = cityName;
    [detailTVC startReqWeatherDirectly];
    
    [[WWCollectedCitiesManage sharedCollectedCities] addCurSelCity:cityId];
}
- (IBAction)AddCities:(id)sender {
    [self.mm_drawerController closeDrawerAnimated:NO completion:nil];
    AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
    UINavigationController *navigationController = (UINavigationController*)appDelegate.drawViewController.centerViewController;
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    [navigationController pushViewController:[storyboard instantiateViewControllerWithIdentifier:@"searchView"] animated:YES];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    if (editing) {
        _addCityButton.enabled = NO;
    } else {
        _addCityButton.enabled = YES;
    }
}

- (IBAction)editCollectedCities:(id)sender {
    if (self.tableView.isEditing) {
        [_editCityButton setTitle:@"编辑"];
    } else {
        [_editCityButton setTitle:@"完成"];
    }
    [self setEditing:!self.tableView.isEditing animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [[WWCollectedCitiesManage sharedCollectedCities] removeCity:indexPath.row];
            //[_collectedCityID removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
@end










































